<template>
  <a-modal
    :title='model ? "编辑页面" : "新增页面"'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-form-item label='id' v-show='false'>
          <a-input v-decorator="['id']" />
        </a-form-item>

        <a-form-item label='页面名称'>
          <a-input v-decorator="[
              'pageName',
              { rules: [
                { required: true, message: '请输入页面名称!' }, { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}] },
            ]" />
        </a-form-item>

        <a-form-item label='是否可见'>
          <a-radio-group :default-value='1' v-decorator="['isHidden',{initialValue: 1}]">
            <a-radio :value='0'>
              否
            </a-radio>
            <a-radio :value='1'>
              是
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { namePattern } from '@/reges/index'

const fields = ['id', 'pageName', 'isHidden']

export default {
  name: 'creatRealform',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  components: {},
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      namePattern,
      templateIndex: 0,
      form: this.$form.createForm(this)
    }
  },
  created() {
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      // console.log()

      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    // validatorPhone (rule, value, callback) {
    //   if (!value) {
    //     callback(new Error('请输入设备名称'))
    //   } else {
    //     if (!/^\S*$/.test(value)) {
    //       callback(new Error('请勿输入空格等特殊字符!'))
    //     } else {
    //       callback()
    //     }
    //   }
    // }
  }
}
</script>

<style lang='less' scoped>
</style>